package org.grayrabbit.cms.publish.generate;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.util.FilePathSptUtil;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 2011-6-15 10:43:13
 */
public class TopicListGenerater extends AbstractGenerater {
	private static String htmlFileName = "list";
	private static String htmlFileNameExt = ".html";
	private static String topicPath = "topic";
	private static int firstPage = 1;
	
	private Topic topic;
	private int currentPage;
	private int totelPage;
	private String baseUrl;
	private List<Content> contents;

	public TopicListGenerater(Configuration configuration, String path, Topic topic , String baseUrl, int currentPage, int totelPage, List<Content> contents,Generatable generatable,Admin admin) {
		super(configuration, path,generatable,admin);
		this.topic = topic;
		this.currentPage = currentPage;
		this.totelPage = totelPage;
		this.baseUrl = FilePathSptUtil.SITE_VISIT_URL+FilePathSptUtil.padRightSpt(topic.getSite().getSiteShortName())+FilePathSptUtil.padRightSpt(topicPath)+topic.getTopicSimpleName()+FilePathSptUtil.padLeftSpt(htmlFileName);
		this.contents = contents;
	}

	@Override
	protected File getTargetFile() {
		return new File(getFilePath(topic));
	}

	@Override
	protected void initRootMap() {
		rootMap.put("topic", topic);
		rootMap.put("currentPage", currentPage);
		rootMap.put("totelPage", totelPage);
		rootMap.put("baseUrl", baseUrl);
		rootMap.put("contents", contents);
		rootMap.put("site",topic.getSite());
	}
	
	private String getFilePath(Topic topic){
		if(firstPage==currentPage){
			return FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+FilePathSptUtil.padRightSpt(topic.getSite().getSiteShortName())+FilePathSptUtil.padRightSpt(topicPath)+topic.getTopicSimpleName()+FilePathSptUtil.padLeftSpt(htmlFileName+htmlFileNameExt);
		}else{
			return FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+FilePathSptUtil.padRightSpt(topic.getSite().getSiteShortName())+FilePathSptUtil.padRightSpt(topicPath)+topic.getTopicSimpleName()+FilePathSptUtil.padLeftSpt(htmlFileName+"_"+currentPage+htmlFileNameExt);
		}
		
	}

	@Override
	protected void logInfo(PublishTask publishTask,boolean success,Throwable t) {
		PublishLog publishLog = new PublishLog();
		publishLog.setHtmlPath(getTargetFile().toString());
		publishLog.setIsError(Boolean.valueOf(!success));
		publishLog.setObjectId(topic.getTopicId());
		if(success){
			publishLog.setPublishInfo("生成成功");
		}else{
			if(t!=null){
				publishLog.setPublishInfo("生成失败:"+t.getMessage());
			}else{
				publishLog.setPublishInfo("生成失败");
			}
		}
		publishLog.setPublishStatus(PublishTask.STATUS_FINISHED);
		publishLog.setPublishTime(new Date());
		publishLog.setPublishType("topiclist");
		publishLog.setTemplatePath(this.path);
		publishLog.setPublishTask(publishTask);
		generatable.savePublishLog(publishLog);
		calcTaskInfo(publishTask,success);
		
	}

	public String toString(){
		return "topiclist:"+topic.getTopicId();
	}
}